//
//  CZEmoticonManager.swift
//  keyBoard
//
//  Created by 李伟鹏 on 16/5/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

 let emoticonBundlePath = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!

class CZEmoticonManager: NSObject {
    
    private let emoticonsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/emoticons.plist"
    
    static let sharedManager: CZEmoticonManager = CZEmoticonManager()
    
    lazy var emoticonPackageModels: [CZEmoticonPackageModel] = self.loadEmoticonPackageModels()
    
    private override init(){
        super.init()
    }
    
    private func loadEmoticonPackageModels() -> [CZEmoticonPackageModel]{
        let recentPackageModel = CZEmoticonPackageModel(id: "", group_name_cn: "最近", emoticons: loadRecentEmoticons())
        let defaultPackageModel = loadEmoticonPackageModel("com.sina.default")
        let emoji = loadEmoticonPackageModel("com.apple.emoji")
        let lxh = loadEmoticonPackageModel("com.sina.lxh")
        return [recentPackageModel,defaultPackageModel,emoji,lxh]
    }
    
    private func loadEmoticonPackageModel(id: String) -> CZEmoticonPackageModel{
        
       
        let infoPath = emoticonBundlePath + "/" + id + "/info.plist"
        let infoDict = NSDictionary(contentsOfFile: infoPath)!
        let group_name_cn = infoDict["group_name_cn"] as! String
        let emoticonsArr = infoDict["emoticons"] as! [[String: String]]
        var emoticonModels = [CZEmoticonModel]()
        for dict in emoticonsArr{
            let emoticonModel = CZEmoticonModel(dict: dict,id: id)
            emoticonModels.append(emoticonModel)
        }
        return CZEmoticonPackageModel(id: id, group_name_cn: group_name_cn, emoticons: emoticonModels)
    }
    
    func addRecentEmoticon(emoitconModel: CZEmoticonModel){
        var pageEmoticon = emoticonPackageModels[0].pageEmotions[0]
        var sameEmoticon: CZEmoticonModel?
        for e in pageEmoticon{
            if (e.emoji != nil && e.emoji == emoitconModel.emoji) || (e.chs != nil && e.chs == emoitconModel.chs){
                sameEmoticon = e
                break
            }
            
        }
        
        if let se = sameEmoticon{
            let index = pageEmoticon.indexOf(se)
            pageEmoticon.removeAtIndex(index!)
        }
        
        pageEmoticon.insert(emoitconModel, atIndex: 0)
        
        if pageEmoticon.count > CZEmoticonNumberOfPage {
            pageEmoticon.removeLast()
        }
        
        emoticonPackageModels[0].pageEmotions[0] = pageEmoticon
        
        NSKeyedArchiver.archiveRootObject(pageEmoticon, toFile:emoticonsPath)
    }
    
    private func loadRecentEmoticons() -> [CZEmoticonModel]{
        if let emoticons = NSKeyedUnarchiver.unarchiveObjectWithFile(emoticonsPath) as? [CZEmoticonModel]{
            return emoticons
        }else{
            return [CZEmoticonModel]()
        }
    }
}
